package com.sky.mapper;

import com.github.pagehelper.Page;
import com.sky.annotation.AutoFill;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.enumeration.OperationType;
import com.sky.vo.SetmealVO;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface SetmealMapper {

    /**
     * 根据分类id查询套餐的数量
     * @param id
     * @return
     */
    @Select("select count(id) from setmeal where category_id = #{categoryId}")
    Integer countByCategoryId(Long id);

    @Options(useGeneratedKeys = true, keyProperty = "id")
    @AutoFill(OperationType.INSERT)
    @Insert("insert into setmeal (category_id, name, price,description ,image ,create_time ,update_time,create_user ,update_user) " +
            "values(#{categoryId},#{name},#{price},#{description},#{image},#{createTime},#{updateTime},#{createUser},#{updateUser}) ")
    void insert(Setmeal setmeal);

    @Insert("insert into setmeal_dish (setmeal_id,dish_id,name,price,copies) values(#{setmealId},#{dishId},#{name},#{price},#{copies})")
    void insertDish(SetmealDish setmealDish);

    Page<SetmealVO> page(SetmealPageQueryDTO setmealPageQueryDTO);

    //根据id查询setmeal_dish里面的代码并响应在页面中
    @Select("select * from setmeal where id = #{id} ")
    SetmealVO findById(Long id);

    @Options(useGeneratedKeys = true, keyProperty = "id")
    SetmealVO  getByIdWithDish(Long id);

    @Delete("delete from setmeal where id = #{id}")
    void delete(Long id);

    @Delete("delete from setmeal_dish where setmeal_id = #{id}")
    void deleteByDishId(Long id);

    void update(Setmeal setmeal);

    @Select("select * from dish where category_id = #{categoryId}")
    Page<SetmealVO> list(Long categoryId);
}
